package cn.mumzone.pos.presenter;

import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.List;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.net.response.PageListResponse;
import cn.mumzone.basic.util.DeviceIdUtils;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.model.GoodsEntity;
import cn.mumzone.pos.model.source.GoodsRepository;
import cn.mumzone.pos.model.source.SettleRepository;
import cn.mumzone.pos.presenter.contract.RedeemContract;
import cn.mumzone.pos.util.BillUtil;

/**
 * Created by qiuzhenhuan on 2019/10/9.
 */

public class RedeemPresenter extends AbsPresenter<RedeemContract.View, GoodsRepository> implements RedeemContract.Presenter {

    SettleRepository settleRepository;

    @Override
    protected void initOtherRepository() {
        super.initOtherRepository();
        settleRepository = new SettleRepository();
    }

    private int mPageNum = 1;

    @Override
    public void loadGoodsList(String keyword, final boolean isRefresh) {
        ApiCallBack<PageListResponse<GoodsEntity>> callBack = new ApiCallBack<PageListResponse<GoodsEntity>>() {
            @Override
            public void onSuccess(PageListResponse<GoodsEntity> response) {
                mView.showSearchGoodsList(response.getList(), isRefresh);
                mPageNum++;
            }

            @Override
            public void onError(BaseResponse response) {
                if (response.getCode() == 504) {
                    mView.showSearchGoodsError(response.getMsg());
                } else {
                    mView.showError(response.getMsg());
                }

            }
        };

        mPageNum = isRefresh ? 1 : mPageNum;
        mRepo.searchIntegralGoodsList(keyword, StringUtils.get(mPageNum), StringUtils.get(AppConstants.PAGE_SIZE), callBack);
        addSubscrebe(callBack);
    }

    @Override
    public void doRedeemGoods(String memberCompanyId, String phone, String integralGoods, List<GoodsEntity> integralList) {
        if(ListUtil.isListEmpty(integralList)){
            mView.showError("请选择换购商品");
            return;
        }
        ApiCallBack<String> callBack = new ApiCallBack<String>() {
            @Override
            public void onSuccess(String s) {
                mView.showRedeemGoodSuccess();
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };
        HashMap<String, String> payTypeMap = new HashMap<>();
        payTypeMap.put(BillUtil.PAY_TYPE_INTEGRAL, integralGoods);
        HashMap<String, String> requestMap = new HashMap<>();
        requestMap.put("memberCompanyId", memberCompanyId);
        requestMap.put("phone", phone);
        requestMap.put("posCode", DeviceIdUtils.getDeviceId());
        requestMap.put("billingUser", memberCompanyId);
        requestMap.put("integralGoods", integralGoods);
        requestMap.put("payType", JSON.toJSONString(payTypeMap));
        requestMap.put("integralList", JSON.toJSONString(integralList));
        requestMap.put("status", "2");
        settleRepository.saveOrderIntegral(requestMap, callBack);
        addSubscrebe(callBack);
    }
}
